package com.boss.asset.dao.impl;

import com.boss.asset.dao.IAssetInfoDao;
import com.boss.asset.entity.BasAssetInfo;
import com.boss.common.enums.IsFiscalEnum;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.sys.utils.SysUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class AssetInfoDaoImpl extends BaseDaoImpl implements IAssetInfoDao {

    private final static String TABLE_NAME = "BAS_ASSET_INFO";

    @Override
    public void insert(BasAssetInfo data) {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO ").append(TABLE_NAME);
        sql.append("(ASSET_ID,BIZ_KEY,ASSET_CODE,FIXED_ASSET_TYPE_CODE,AGENCY_CODE,")
                .append("MOF_DIV_CODE,FIXED_ASSET_STATE_CODE,OWNERSHIP_NATURE_CODE,OWNERSHIP_FORM_CODE,DISPOSAL_FORM_CODE,")
                .append("ASSET_AUTH_NO,NUM,NUM_UNIT,PUR_APPROV_UNIT,FIXED_ASS_ACQ_CODE,")
                .append("DIPOS_CHANNEL_CODE,USE_RESPON_SUBJECT_CODE,ASSET_AUTH_SITU_CODE,APPROV_STA_CODE,INIT_ASSET_VAL,")
                .append("ACC_DEP,NET_VAL,FUND_SOURCE,PUR_ORG_FORM,DIPOS_EARN,")
                .append("EARN_SITU,ASSET_NAME,START_DATE,END_DATE,IS_ENABLED,")
                .append("VALUE_TYPE_CODE,UPDATE_TIME,IS_DELETED,CREATE_TIME,PRO_CODE)");
        sql.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?)");

        Object[] params = new Object[]{
                data.getAssetId(), data.getBizKey(), data.getAssetCode(), data.getFixedAssetTypeCode(), data.getAgencyCode(),
                data.getMofDivCode(), data.getFixedAssetStateCode(), data.getOwnershipNatureCode(), data.getOwnershipFormCode(), data.getDisposalFormCode(),
                data.getAssetAuthNo(), data.getNum(), data.getNumUnit(), data.getPurApprovUnit(), data.getFixedAssAcqCode(),
                data.getDiposChannelCode(), data.getUseResponSubjectCode(), data.getAssetAuthSituCode(), data.getApprovStaCode(), data.getInitAssetVal(),
                data.getAccDep(), data.getNetVal(), data.getFundSource(), data.getPurOrgForm(), data.getDiposEarn(),
                data.getEarnSitu(), data.getAssetName(), data.getStartDate(), data.getEndDate(), data.getIsEnabled(),
                data.getValueTypeCode(), data.getUpdateTime(), data.getIsDeleted(), data.getCreateTime(), data.getProCode()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public List<Map<String, Object>> getAssetInfo(String agencyCode, String province, String year) {

        String endDate = year + "1231";
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT info.asset_id as id, info.*, ext.* FROM ").append(TABLE_NAME)
                .append(" info, BAS_ASSET_EXT ext WHERE info.asset_id=ext.asset_id ")
                .append(" and info.start_date <'").append(endDate).append("' and info.end_date >= '")
                .append(endDate).append("' and mof_div_code ='").append(province).append("'")
        .append(" and ext.is_fiscal='").append(IsFiscalEnum.YES.getValue()).append("'");
        if (!SysUtils.isNullOrEmpty(agencyCode)) {
            sql.append(" and info.agency_code = '").append(agencyCode).append("'");
        }
        sql.append(" ORDER BY info.UPDATE_TIME DESC ");
        //请求参数初始化
        return queryForList(sql.toString(), new Object[]{});
//        return this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper(BasAssetBiz.class));
    }

    @Override
    public void dropAssetInfoByIds(List<String> idList) {
        StringBuilder sql = new StringBuilder();
        sql.append("delete from ").append(TABLE_NAME).append(" where ").append(com.boss.sys.utils.SysUtils.inSql("asset_id", idList));
        this.getJdbcTemplate().update(sql.toString(), new Object[]{});
    }
}
